// 停用词表
const stopWords = new Set([
  "的",
  "为",
  "了",
  "是",
  "在",
  "我",
  "有",
  "和",
  "就",
  "不",
  "都",
  "上",
  "也",
  "很",
  "到",
  "说",
  "要",
  "去",
  "你",
  "会",
  "着",
  "没有",
  "看",
  "好",
  "自己",
  "这",
  "它",
  "与",
  "们",
  "断",
  "让",
  "从",
  "而",
  "及",
  "将",
  "涉",
  "来",
]);

/**
 * @param {Array} words  文章文本
 * @return {Array} 处理后的分词数组
 */
function wordSegmentation(words) {
  // TODO：待补充代码
  words = words
    .replace(/[\s\w]/g, "")
    .replace(/[·~！@#￥%……&*（）【】、：；”“‘’《》，。？]/g, "|");
  stopWords.forEach((item) => {
    words = words.replaceAll(item, "|");
  });
  return words.split("|").filter((item) => item != "");
}

/**
 * @param {string} text1 查询文章
 * @param {string} text2 对比文章
 * @return {number} plagiarismRate  相似度
 */
function checkPlagiarism(text1, text2) {
  // 1. 分词
  words1 = wordSegmentation(text1);
  words2 = wordSegmentation(text2);

  // 2.对分词后的原始文本和待比较文本进行去重处理
  const uniqueWords1 = Array.from(new Set(words1)); // 原文分词后的数组
  const uniqueWords2 = Array.from(new Set(words2)); //对照文章分词后的数组

  // 3.统计两篇文章的相同词语数目
  let commonWordsCount = 0;
  uniqueWords1.forEach((word) => {
    if (uniqueWords2.includes(word)) {
      commonWordsCount++;
    }
  });

  // 4.计算重复率
  const plagiarismRate = commonWordsCount / uniqueWords1.length;
  return plagiarismRate;
}
